#ifndef _BC_UTILS_H
#define _BC_UTILS_H

/******************************************************************************
** @file	bc_utils.h
** @author	Arturo Aguilar
** @author	Andrea Herbas
** @date	Ago 29, 2010
**
** @desc	This class encapsules methods that are used by the 
**			bridget_communicator class.
**			
**
******************************************************************************/

#include <string>
#include <iostream>
#include <wmi_reader.h>
#include <cm_resultcollection.h>
#include "BC_Comparator.h"

class bc_utils
{
public:
	/**
	* Template method that compares two versions of a PC data and returns only 
	* the differences between them.
	*
	* @param the most actual version of a PC data.
	* @param the before last version of a PC data.
	* @param output parameter, where all the differences are stored.
	*/
    template <typename T>
    static void 
    fillDifferences(WMI_Result<T>& currentItems, WMI_Result<T>& beforeCurrentItems, 
					WMI_Result<T>& differences)
    {
        for(int j = 0; j < currentItems.getObjects().size(); j++)
        {
            for(int i = 0; i < beforeCurrentItems.getObjects().size(); i++)
            {
                if((*(currentItems.getObjects()[j])).getName() == (*(beforeCurrentItems.getObjects()[i])).getName())
                {
                    BC_Comparator<T> comparator;
                    if(!comparator.compare(currentItems.getObjects()[j], beforeCurrentItems.getObjects()[i]))
                    {
                        T* data = new T(*(beforeCurrentItems.getObjects()[i]));
                        differences.getObjects().push_back(data);
                    }                    
                }
            }
        }   
    }


    static void 
	fillRegistryDifferences(WMI_Result<WMI_Registry>& currentItems, WMI_Result<WMI_Registry>& beforeCurrentItems, 
					WMI_Result<WMI_Registry>& differences)
    {
        WMI_Registry* lastReg = currentItems.getParentRegistry();
        WMI_Registry* beforelastReg = beforeCurrentItems.getParentRegistry();
        BC_Comparator<WMI_Registry> comparator;

        if (!comparator.compare(lastReg, beforelastReg))
        {
            differences.setWRegistry(lastReg);
        }          
    }
	
	/**
	* Method that compares two versions of a PC data. Compares only WMI_FileDirectory instances
	* and returns the differences between them.
	*
	* @param the most actual version of a PC WMI_FileDirectory instances.
	* @param the before last version of a PC WMI_FileDirectory instances.
	* @param output parameter, where all the differences are stored.
	*/
	static void
	fillDifferences(WMI_Result<WMI_FileDirectory>& currentItems, 
					WMI_Result<WMI_FileDirectory>& beforeCurrentItems, 
					WMI_Result<WMI_FileDirectory>& differences);
	/** 
    * Initializes the necessary connection type to retrieve data from a computer
    *
    * @param output parameter, that encapsulates the connection.
    * @param user name on the computer that we want to access.
    * @param user password
    * @param ip address of the computer that we want to access.
    * @param false if the connection needed is for retrieve registries, true is used
    *        for the rest of the objects.
    * @param true if the connection is local, otherwise false (remote connection). 
    * @return a flag that indicates the state of the transaction.
    */
    static WMI_HResult  
    initConnection(WMI_Connection& connection, wstring& user, wstring& password, 
				   wstring& host, bool connectionType, bool localActiv);

	/**
    * Obtains all the process on a PC and put them on the collection.
    *
    * @param user name of the computer that we want to access.
    * @param user password
    * @param ip address of the computer that we want to access.
    * @param false if the connection needed is for retrieve registries, true is used
    *        for the rest of the objects.
    * @param true if the connection is local, otherwise false (remote connection). 
    * @return a flag that indicates the state of the transaction.
    */
    static void 
    getProcess(WMI_Connection& wmiConnection, CM_ResultCollection& resultCollection);

	/**
    * Obtains all the shares on a PC and put them on the collection.
    *
    * @param user name of the computer that we want to access.
    * @param user password
    * @param ip address of the computer that we want to access.
    * @param false if the connection needed is for retrieve registries, true is used
    *        for the rest of the objects.
    * @param true if the connection is local, otherwise false (remote connection). 
    * @return a flag that indicates the state of the transaction.
    */
    static void 
    getShares(WMI_Connection& wmiConnection, CM_ResultCollection& resultCollection);

	/**
    * Obtains all the registries on a PC and put them on the collection.
    *
    * @param user name of the computer that we want to access.
    * @param user password
    * @param ip address of the computer that we want to access.
    * @param false if the connection needed is for retrieve registries, true is used
    *        for the rest of the objects.
    * @param true if the connection is local, otherwise false (remote connection). 
    * @return a flag that indicates the state of the transaction.
    */
    static void 
    getRegistries(WMI_Connection& wmiConnection, const wstring& path, CM_ResultCollection& resultCollection);

	static void 
	getFileDirectory(WMI_Connection& wmiConnection, const wstring& path, CM_ResultCollection& resultCollection);
};

#endif